﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Gui.CompletionWindow;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.TextEditor.Actions;
using Bing.Toolkit.T4;

namespace Bing.Studio
{
    public partial class FrmCodeGenerator : BaseForm
    {
        private string _strDatabaseName;
        private string _strTableName;
        private string _strConnectionString;
        private string _providerName;

        private TableObject _tblInfomation;

        public FrmCodeGenerator(string strDatabaseName, string strTableName, string strConnectionString, string providerName)
        {
            _strDatabaseName = strDatabaseName;
            _strTableName = strTableName;
            _strConnectionString = strConnectionString;
            _providerName = providerName;

            InitializeComponent();
        }

        /****************************************************************************************************/

        #region /*自定义方法*/

        public void FillTemplateContent(string content)
        {
            this.textEditorControl1.Text = content;
        }

        public void RunCodeGenerator()
        {
            if (!String.IsNullOrEmpty(textEditorControl1.Text))
            {
                var strErrorMessage = String.Empty;
                var host = new TableHost(_tblInfomation);
                var strCode = T4Engine.BuilderText(textEditorControl1.Text, host, out strErrorMessage);

                if (strErrorMessage.Length > 0)
                {
                    SendNotification(String.Join("\r\n\r\n", strErrorMessage));
                }
                else
                {
                    this.textEditorControl2.Text = strCode;
                    this.tabControl1.SelectedIndex = 1;
                    SendNotification(this.groupBox1.Text + "编译成功.");
                }
            }
            else
            {
                SendNotification("请通过文件对象管理器获取模板代码.");
            }
        }

        #endregion

        /****************************************************************************************************/

        private void FrmCodeGenerator_Load(object sender, EventArgs e)
        {
            groupBox1.Text += String.Format("{0}->{1}", _strDatabaseName, _strTableName);

            var db = new SQLServerDataStructuresHelper(_strConnectionString);

            _tblInfomation = db.GetTableInfomation(_strTableName);

            dgvDataColumn.DataSource = _tblInfomation.Columns;

            this.textEditorControl1.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C#");
            this.textEditorControl1.Encoding = Encoding.Default;
            this.textEditorControl2.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C#");
            this.textEditorControl2.Encoding = Encoding.Default;
        }

        /****************************************************************************************************/
    }
}
